import Validator from '../validator/index'

module.exports = {
  /**
   * @desc 设置错误提示
   */
  setErrorMsg(errorMsg) {
    errorMsg = errorMsg ? `* ${errorMsg}` : ''

    this.setData({ errorMsg })
  },

  /**
   * @desc 清空错误提示
   */
  clearErrorMsg() {
    this.setErrorMsg('')
  },

  /**
   * @desc 输入框事件
   */
  oninput(e) {
    let {
      detail: { value },
      currentTarget: { dataset: { value: key } },
    } = e
    let data = {}

    data[key] = value
    this.setData(data)
  },

  /**
   * @desc 验证
   */
  validate(data) {
    let validator = new Validator()

    data.forEach(({ value, strategies }) => validator.add(value, strategies))

    return validator.start()
  },
}